<template>
	<view class="wrap">
		<u-navbar :custom-back="$back" :back-icon-color="navbar.titleColor" is-back :title="navbar.title"
			:background="navbar.background" :title-color="navbar.titleColor">
		</u-navbar>

		<view class="content">
			<view class="header">
				<view class="header-top">
					<view class="list-item" @click="listStatus(null)">
						<view class="list-name">
							全部总数
						</view>
						<view class="list-value">
							{{total}}
						</view>
					</view>
				</view>
				<view class="header-list">
					<view class="list-item box" v-if="index <= 3" v-for="item,index in totalList" :key="item.name" @click="listStatus(item.value)">
						<view class="list-value" :style="'color:' + item.color">
							{{item.valueNum}}
						</view>
						<view class="list-name">
							{{item.name}}
						</view>
					</view>
				</view>
				<view class="header-list">
					<view class="list-item box" v-if="index > 3" v-for="item,index in totalList" :key="item.name" @click="listStatus(item.value)">
						<view class="list-value" :style="'color:' + item.color">
							{{item.valueNum}}
						</view>
						<view class="list-name">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
			<view class="toolbar">
				<!-- <u-grid class="grid" :col="1" :border="false">
					<u-grid-item class="grid-item">
						<view class="grid-text-title">来访申请审批管理</view>
					</u-grid-item>
				</u-grid> -->
				<u-grid class="grid" :col="3" :border="false">
					<view style="margin-bottom: 20upx;"></view>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/add')" v-if="visitorAddCheck">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266429201775329280/ic_visitor_add.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">后台录入申请单</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266429334520856576/ic_visitor_all_list.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">所有申请单</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=0')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266429564997861376/ic_visitor_daishenpi.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">待审批</view>
					</u-grid-item>
				</u-grid>
				<u-grid class="grid" :col="3" :border="false">
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=2')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266429935161966592/ic_visitor_dailaifang.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">审批通过待来访</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=1')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266430206596349952/ic_visitor_weitongguo.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">审批未通过</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=3')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266430361265504256/ic_visitor_yilaifang.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">已来访</view>
					</u-grid-item>
				</u-grid>
				<u-grid class="grid" :col="3" :border="false">
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=4')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266430573505675264/ic_visitor_weishenguoqi.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">未审过期</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=5')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266430643269533696/ic_visitor_yishenguoqi.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">已审过期</view>
					</u-grid-item>
					<u-grid-item class="grid-item" @click="homeNavigateTo('/subPages/visitor/index?visitorInfoStatus=6')">
						<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266430913747615744/ic_visitor_yiliyuan.png" class="icon" alt="" mode="widthFix"/>
						<view class="grid-text">已离园</view>
					</u-grid-item>
				</u-grid>
			</view>
		
			<view class="content-list">
				<view class="content-box">
					<view class="content-item" @click="goDetail(item)" v-for="item in dataList">
						<view class="item-row">
							<view class="item-row-title">
							  <view class="image-wrapper">
								<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266446434819899392/ic_visitor_ren.png" alt="">
							  </view>
							  <text class="text">{{item.visitName}}</text>
							</view>
							<!-- 状态 -->
							<text v-for="item2,index in totalList" :key="item2.name" v-if="item.visitorInfoStatus == item2.value" :style="'background-color:' + item2.color + ';color:#FFF;padding: 10rpx;font-size: 25upx;'">{{item2.name}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise"><text class="item-huise-title">来访时间：</text>{{item.visitDate}} {{item.visitTimePoint}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise"><text class="item-huise-title">被访人名称：</text>{{item.visitPersonName}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise"><text class="item-huise-title">被访人电话：</text>{{item.visitPersonPhone}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise"><text class="item-huise-title">来访事由：</text>{{item.visitSubjectMatter}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise">
								<text class="item-huise-title">录入类型：</text>
								{{item.filledUserType == 0 ? '访客录入' : '后台录入'}}
							</text>
							<text class="item-huise" v-if="item.filledUserType == 1">
								<text class="item-huise-title">录入人员：</text>
								{{item.filledUserName}}
							</text>
						</view>
						<view class="item-row" v-if="item.visitorInfoStatus == 3">
							<text class="item-huise"><text class="item-huise-title">实际到访时间：</text>{{item.realityVisitTime}}</text>
						</view>
						<view class="item-row">
							<text	v-if="item.visitorInfoStatus == 0 && (allowCheck || refuseCheck)" class="item-huise"><text class="item-huise-title">操作：</text></text>
							<button v-if="item.visitorInfoStatus == 0 && allowCheck" @click.stop.prevent="allow(item)"
								style="margin-right: -200upx;line-height:2;" type="primary" size="mini">设为审批通过</button>
							<button v-if="item.visitorInfoStatus == 0 && refuseCheck" @click.stop.prevent="refuse(item)"
								style="margin-right: 0;line-height:2;" type="warn" size="mini">设为未通过</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//头部标题配置
				navbar: {
					title: " 访客管理",
					background: {
						backgroundColor: '#2373FA',
					},
					titleColor: "#ffffff",
				},
				
				totalList: [{
					name: '全部',
					value: null,
					valueNum: 0,
					color: '#000000'
				},{
					name: '未审核',
					value: 0,
					valueNum: 0,
					color: '#FFAA01'
				}, {
					name: '未通过',
					value: 1,
					valueNum: 0,
					color: '#FF3939'
				}, {
					name: '待来访',
					value: 2,
					valueNum: 0,
					color: '#13B3FD'
				}, {
					name: '已来访',
					value: 3,
					valueNum: 0,
					color: '#469D50'
				}, {
					name: '未审过期',
					value: 4,
					valueNum: 0,
					color: '#505050'
				}, {
					name: '已审过期',
					value: 5,
					valueNum: 0,
					color: '#505050'
				}, {
					name: '已离园',
					value: 6,
					valueNum: 0,
					color: '#0055ff'
				}],
				dataList: [],
				total: 0,
				// 优先级数据源
				priorityList: [{
					text: '一般',
					value: 1
				}, {
					text: '紧急',
					value: 2
				}],
				user: {},
				visitorAddCheck: false,
				allowCheck: false,
				refuseCheck: false,
			};
		},
		onShow() {
			this.initPermissions()
			this.checkUser()
			this.getVisitorInfoNum(null)
			this.getVisitorInfo(null)
		},
		methods: {
			// 初始化权限
			initPermissions(){
				this.visitorAddCheck = false,
				this.allowCheck = false,
				this.refuseCheck = false,
				this.$u.api.getInfo().then(res => {
					let permissions = res.permissions;
					for (var i = 0; i < permissions.length; i++) {
						let permission = permissions[i];
						if(permission == 'visitor:visitorInfo:add'){
							this.visitorAddCheck = true;
						}
						if(permission == 'visitor:visitorInfo:allow'){
							this.allowCheck = true;
						}
						if(permission == 'visitor:visitorInfo:refuse'){
							this.refuseCheck = true;
						}
					}
				})	
			},
			checkUser(){
				this.user = this.$store.state.vuex_user;
				console.log('user',this.user);
				if(this.user.userName){
					
				}
			},
			listStatus(status){
				this.getVisitorInfo(status)
			},
			groupBy(array, key) {
			  return array.reduce((result, currentItem) => {
			    // 使用 key 函数获取分组的键
			    const groupKey = key(currentItem);
			 
			    // 确保 result 对象中有对应键的数组
			    if (!result[groupKey]) {
			      result[groupKey] = [];
			    }
			 
			    // 将当前项添加到对应分组的数组中
			    result[groupKey].push(currentItem);
			 
			    return result;
			  }, {});
			},
			homeNavigateTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			getVisitorInfoNum(status) {
				let params = {
					pageNum: 1,
					pageSize: 200,
					visitorInfoStatus: status
				};
				if(status == null){
					params = {
						pageNum: 1,
						pageSize: 200
					};
				}else{
					params = {
						pageNum: 1,
						pageSize: 200,
						visitorInfoStatus: status
					};
				}
				
				this.$u.api.visitor.list(params).then(res => {
					this.dataList = res.rows;
					this.total = res.total;
					this.totalList[0].valueNum = this.total;
					const groupedItems = this.groupBy(this.dataList, visitorInfo => visitorInfo.visitorInfoStatus);
					for (let i = 0; i < this.totalList.length; i++) {
						let items = groupedItems[this.totalList[i].value]
						if(items){
							this.totalList[i].valueNum = items.length;
						}
					}
				})
			},
			getVisitorInfo(status) {
				let params = {
					pageNum: 1,
					pageSize: 200,
					visitorInfoStatus: status
				};
				if(status == null){
					params = {
						pageNum: 1,
						pageSize: 200
					};
				}else{
					params = {
						pageNum: 1,
						pageSize: 200,
						visitorInfoStatus: status
					};
				}
				this.$u.api.visitor.list(params).then(res => {
					this.dataList = res.rows;
				})
			},
			goDetail(item) {
				setTimeout(() => {
					uni.navigateTo({
						url: `/subPages/visitor/visitorInfoDetail?visitorInfoId=${item.visitorInfoId}`
					})
				}, 300)
			},
			
			getPriority(value) {
				return this.priorityList.find(d => d.value == value)?.text
			},
			refuse(item) {
				// 调用审批不通过接口
				this.$u.api.visitor.refuse({'visitorInfoId':item.visitorInfoId}).then(async res => {
					console.log(res)
					this.getVisitorInfo(null)
				})
			},
			allow(item) {
				// 调用审批通过接口
				this.$u.api.visitor.allow({'visitorInfoId':item.visitorInfoId}).then(async res => {
					console.log(res)
					this.getVisitorInfo(null)
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #f2f4f8;
	}

	.wrap {
		padding-bottom: 5vh;
		overflow: hidden;

		.content {

			.header {
				background-color: #fff;
				padding: 30upx 0;

				.header-top {
					.list-item {
						display: flex;
						align-items: center;
						flex-direction: column;

						.list-name {
							font-size: 30upx;
							color: #9e9d9a;
						}

						.list-value {
							font-size: 60upx;
							color: #2373FA;
							font-weight: 500;
						}
					}
				}

				.header-list {
					display: flex;
					margin-top: 10upx;
					
					.box {
						// background-color: #17ffa6;
						margin: 5upx;
					}
					
					.list-item {
						flex: 1;
						display: flex;
						align-items: center;
						flex-direction: column;
						font-size: 25upx;

						.list-name {
							color: #9e9d9a;
						}

						.list-value {
							margin-top: 10upx;
							color: #2373FA;
							font-weight: 500;
						}
					}
					.list-item2 {
						font-size: 25upx;

						.list-name {
							color: #9e9d9a;
						}

						.list-value {
							margin-top: 10upx;
							color: #2373FA;
							font-weight: 500;
						}
					}
				}
			}

			.toolbar {
				background-color: #fff;
				margin: 15px 0;

				.grid {
					.grid-icon {
						color: #666;
					}
					.icon {
						width: 100rpx;
						border-radius: 10px;
					}

					.grid-item {
						// padding: 20rpx;
						/deep/.u-grid-item-box {
							padding: 0px !important;
						}
					}

					.grid-text {
						height: 75upx;
						text-align: center;
					}

					.grid-text {
						font-size: 25rpx;
						color: #5B5B5B;
						padding: 15rpx;
					}
					.grid-text-title {
						text-indent: 2em;;
						width: 100%;
						text-align: left;
						font-size: 25rpx;
						font-weight: bold;
						color: #5B5B5B;
						padding-top: 15rpx;
						padding-bottom: 15rpx;
					}
				}
			}

			.content-list {

				.content-header {
					display: flex;
					justify-content: space-between;
					padding: 15upx;
					font-size: 35upx;
					font-weight: bold;
				}

				.content-box {
					padding: 0 15upx 15upx 15upx;
					font-size: 30upx;
					font-weight: bold;

					.content-item {
						background-color: #fff;
						border-radius: 5px;
						padding: 10px;
						position: relative;
						border-top: 1px solid #f2f4f8;
						margin-top: 15upx;
						display: flex;
						flex-direction: column;
						box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

						>text {
							position: absolute;
							right: -15upx;
							top: 0;
							background-color: #B9E1FB;
							color: #2B96C6;
							font-size: 25upx;
							padding: 5upx 10upx;
							border-bottom-left-radius: 5px;
						}

						.item-row {
							display: flex;
							justify-content: space-between;
							height: 25px;
							align-items: center;

							&:first-child {
								border-bottom: 1px solid #f2f4f8;
							}
							
							.item-row-title{
								font-size: 25upx;
								display: flex;
								align-items: center; /* 垂直居中 */
								justify-content: center; /* 水平居中 */
								
								.image-wrapper {
								  margin-right: 0px; /* 可以调整间距 */
								}
								image {
									width: 40rpx;height: 40rpx;
								}
							}

							.item-huise {
								font-size: 20upx;
								color: #9e9d9a;
								
								.item-huise-title {
									font-size: 20upx;
								}
								
							}

							.noSX {
								flex-shrink: 0;
							}

							/deep/.u-progress {
								flex: 1;
								height: 8px !important;
								margin: 0 10px;
							}

							.gao {
								background-color: #ffaa00;
								border-radius: 10rpx;
								padding: 0 10rpx;
								display: inline-block;
								color: #fff;
								font-size: 25upx;
								line-height: 40upx;
								font-weight: 100;
								text-align: center;
								margin-right: 10upx;
							}

							.priority1 {
								background-color: #4CB2DB;
							}

							.priority2 {
								background-color: #fa3534;
							}

							/deep/.u-tag {
								margin-left: 10upx;
								padding: 4px;
								font-size: 11px;
							}
						}
					}
				}
			}
		}
	}
</style>